home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Snippets / QD3D Juggler / Juggler Sources / CCameraMover.cp < prev    next >
Encoding:
Text File  |  1995-12-27  |  2.1 KB  |  83 lines  |  [TEXT/CWIE]

  1. //
  2. //    CCameraMover.cp
  3. //
  4. //    class CCameraMover
  5. //    Moves a camera given changes in spherical coordinate values.
  6. //
  7. //    by James Jennings
  8. //    November 26, 1995
  9. //
  10.  
  11. #include "CCameraMover.h"
  12. #include "QD3D Debug Macros.h"
  13.  
  14. // the about to avoid the z-axis by so our math won't explode
  15. const float zAvoid = kQ3Pi/18;    // 10 degrees
  16.  
  17. CCameraMover::CCameraMover(TQ3ViewObject aView)
  18. {
  19.     ThrowIfQ3Fail_( ::Q3View_GetCamera(aView, &mCamera) );
  20.     TQ3Status status = ::Q3Camera_GetPlacement(mCamera, &mPlacement);
  21.     if (status==kQ3Failure) {
  22.         ::Q3Object_Dispose(mCamera);
  23.         ThrowIfQ3Fail_(status);
  24.     }
  25. #if 0    // Note: the debugging version of QD3D is wrong here.
  26.     ::Q3Point3D_ToSpherical(&(mPlacement.cameraLocation), &mLocation);
  27. #else    // so we supply an alternate definition
  28.     TQ3Vector3D *v = (TQ3Vector3D*)&(mPlacement.cameraLocation);
  29.     mLocation.rho = ::Q3Vector3D_Length(v);
  30.     if (mLocation.rho==0) {
  31.         mLocation.phi = 0;
  32.         mLocation.theta = 0;
  33.     } else {
  34.         mLocation.phi    = ::acos(v->z/mLocation.rho);
  35.         if (v->x==0 && v->y==0)
  36.             mLocation.theta = 0;
  37.         else
  38.             mLocation.theta = ::atan2(v->y, v->x);
  39.     }
  40. #endif
  41. }
  42.  
  43. CCameraMover::~CCameraMover()
  44. {
  45.     if (mCamera)
  46.         ::Q3Object_Dispose(mCamera);
  47. }
  48.  
  49. void
  50. CCameraMover::AdjustUpVector()
  51. {
  52.     TQ3Vector3D up, tmp, *loc;
  53.     ::Q3Vector3D_Set(&up, 0, 0, 1);
  54.     loc = (TQ3Vector3D*)&(mPlacement.cameraLocation);    // type casting pointer
  55.     // gs = Gramm-Scmit orthogonalization factor
  56.     float gs = ::Q3Vector3D_Dot(loc, &up) / ::Q3Vector3D_Length(loc);
  57.     ::Q3Vector3D_Subtract(
  58.             &up, 
  59.             ::Q3Vector3D_Scale(loc, gs, &tmp),
  60.             &up);
  61.     ::Q3Vector3D_Normalize(&up, &up);
  62.     // check for nil vector
  63.     
  64.     mPlacement.upVector = up;
  65. }
  66.  
  67. void
  68. CCameraMover::Move(float deltaTheta, float deltaPhi)
  69. {
  70.     mLocation.theta += deltaTheta;
  71.     mLocation.phi   += deltaPhi;
  72.     // clip the result: 0+ < phi < 180-, 0 < theta < 360
  73.     if (mLocation.phi > kQ3Pi-zAvoid)    mLocation.phi = kQ3Pi-zAvoid;
  74.     if (mLocation.phi < 0+zAvoid)        mLocation.phi = zAvoid;
  75.     mLocation.theta = ::fmod(mLocation.theta+kQ32Pi, kQ32Pi);
  76.     // store it
  77.     ::Q3SphericalPoint_ToPoint3D(&mLocation, &(mPlacement.cameraLocation));
  78.     AdjustUpVector();
  79.         
  80.     ThrowIfQ3Fail_( ::Q3Camera_SetPlacement(mCamera, &mPlacement) );
  81. }
  82.  
  83.